package com.apes.project.jekunAuto.timeTask.vip;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.crm.vip.base.VipLevel;
import com.apes.crm.vip.base.VipLiveness;
import com.apes.crm.vip.base.VipType;
import com.apes.crm.vip.vipInfo.VipInfo;
import com.apes.crm.vip.vipInfo.VipInfoRepository;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.generator.service.SequenceService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.project.jekunAuto.timeTask.BaseService;
import com.apes.scm.masterdata.contrast.model.ContrastPartyCode;
import com.apes.scm.masterdata.contrast.repository.ContrastPartyCodeRepository;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.model.PartyIdentification;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.model.Person;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.party.service.PartyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

@Service("vipSynService")
public class VipSynService extends BaseService {


    @Autowired
    PartyRoleRepository partyRoleRepository;

    @Autowired
    ContrastPartyCodeRepository contrastPartyCodeRepository;

    @Autowired
    public SequenceService sequenceService;

    @Autowired
    PartyService partyService;

    @Autowired
    VipInfoRepository vipInfoRepository;



    /*
    * 同步会员信息
    * */
    public JSONArray synVipInfos(SimpleRequest simpleRequest) {
        return getData("ipmVipService.getVipList");
    }

    /*
    *  只同步老系统非会员信息和会员信息 VIPLXID 01 02
    * */
    public Party saveVipInfo(SimpleRequest simpleRequest) throws Exception {
        JSONObject simpleRequestObj = simpleRequest.getJO();
        // 老系统会员编码
        String vipId = simpleRequestObj.getString("VIPID");
        String name = simpleRequestObj.getString("VIPXM");
        String vipTypeId = simpleRequestObj.getString("VIPLXID");

        // 单位会员 内部会员暂不同步
        if (!("01".equals(vipTypeId) || "02".equals(vipTypeId))) {
            return null;
        }
        Party party = simpleRequest.getO(Party.class);
        Person person = simpleRequest.getO(Person.class);
        Party oldParty;
        // 判断会员是否存在, 先判断对照表,再判断手机号码唯一标识是否存在
        ContrastPartyCode contrastPartyCode = contrastPartyCodeRepository.findFirstByVipid(vipId);
        if(contrastPartyCode != null) {
            oldParty = contrastPartyCode.getParty();
        }else {
            oldParty = partyService.findPartyByIdTypeAndIdValue("PIT_mobile",person.getMobileNumber());
            contrastPartyCode = new ContrastPartyCode();
        }
        if(oldParty != null) {
            party.setId(oldParty.getId());
            // 属于员工的, 名字用原来的
            if(oldParty.isRole("Employee")) {
                party.setName(oldParty.getName());
            }
            party.setVersion(oldParty.getVersion());
            Set<PartyRole> partyRoles = new HashSet<>(oldParty.getPartyRoles());
            party.setPartyRoles(partyRoles);
            Set<PartyIdentification> partyIdentifications = new HashSet<>(oldParty.getPartyIdentifications());
            party.setPartyIdentifications(partyIdentifications);
            Person oldPerson = oldParty.getPerson();
            if(oldPerson != null){
                person.setId(oldPerson.getId());
            }
        }
        // 设置为个人
        party.setPartyType("person");
        // 性别
        Enumeration sex = new Enumeration();
        String sexString = simpleRequestObj.getString("XB");
        if ("女".equals(sexString)) {
            sex.setId("000149");
        } else {
            sex.setId("000150");
        }
        person.setSex(sex);
        party.setPerson(person);
        String code = partyService.findRoleCodeByRole(party,"Customer");
        // 不存在客户编码
        if (code == null) {
            //code = sequenceService.getId("PARTY_Customer");
            // 取原yyerp会员编码
            code = vipId;
            party.addPartyRole("Customer",vipId);
        }
        // 更新或者保存party信息
        party = partyService.saveAndFlush(party, "Customer", code);

        // 更新或保存 vipInfo 信息
        if ("02".equals(vipTypeId)) {
            VipInfo vipInfo = insertVipInfo(party , simpleRequestObj);
            vipInfo.setParty(party);
            vipInfoRepository.saveAndFlush(vipInfo);
        }

        // 更新对照表
        contrastPartyCode.setParty(party);
        contrastPartyCode.setVipid(vipId);
        contrastPartyCode.setKhid(simpleRequestObj.getString("KHID"));
        contrastPartyCodeRepository.save(contrastPartyCode);

        return party;

    }

    /*
    *  创建会员类型等信息
    * */
    private VipInfo insertVipInfo(Party party,JSONObject simpleRequestObj) {

        // 查询会员是否存在
        VipInfo vipInfo = vipInfoRepository.findFirstByPartyEquals(party);

        if(vipInfo == null) {
            vipInfo = new VipInfo();
        }

        //会员类型
        VipType vipType = new VipType();
        vipType.setId(simpleRequestObj.getString("VIPLXID"));
        vipInfo.setVipType(vipType);
        // 会员级别
        VipLevel vipLevel = new VipLevel();
        vipLevel.setId(simpleRequestObj.getString("VIPJBID"));
        vipInfo.setVipLevel(vipLevel);
        // 会员活跃度
        VipLiveness vipLiveness = new VipLiveness();
        vipLiveness.setId(simpleRequestObj.getString("VIPHYDID"));
        vipInfo.setVipLiveness(vipLiveness);
        // 注册途径
        Enumeration registerWay = new Enumeration();
        int  registerWayId = simpleRequestObj.getInteger("ZCTJ");
        if( registerWayId == 0 ) { // 门店
            registerWay.setId("000042");
        } else if( registerWayId == 1 )  { //官网
            registerWay.setId("000044");
        } else if( registerWayId == 2 ) { // 微信
            registerWay.setId("000041");
        } else if( registerWayId == 3 ) { // app
            registerWay.setId("000043");
        } else if( registerWayId == 4 ) { // crm
            registerWay.setId("000207");
        } else {
            registerWay.setId("000042");
        }
        vipInfo.setRegisterWay(registerWay);
        // 会员状态
        Enumeration vipState = new Enumeration();
        int vipStateId = simpleRequestObj.getInteger("VIPZT");
        if ( vipStateId == 0 ) { //冻结
            vipState.setId("000176");
        } else if ( vipStateId == 1 ) { // 启用
            vipState.setId("000177");
        } else {
            vipState.setId("000176");
        }
        vipInfo.setVipState(vipState);
        String mdid = simpleRequestObj.getString("MDID");
        if(mdid != null) {
            Dept dept = SpringManager.getBean(DeptRepository.class).findOne(JpaDsl.toCriteriaByEq(
                    "origin", mdid
            )).orElse(null);
            vipInfo.setServeDept(dept);
        }
        // 没有注册门店
        if(vipInfo.getRegDept() == null) {
            vipInfo.setRegDept(vipInfo.getServeDept());
        }

        return vipInfo;
    }
}
